package com.example.eldersystem.controller;


import com.example.eldersystem.operatedata.model.SystemUser;
import com.example.eldersystem.response.DataResponse;
import com.example.eldersystem.operatedata.model.Employee;
import com.example.eldersystem.operatedata.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;


@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    EmployeeService employeeService;

    @RequestMapping("/add")
    private DataResponse<String> addEmployee(@RequestBody Map<String,Object> map){
        DataResponse<String> dataResponse = new DataResponse<>();
        Employee employee = new Employee();

        Date udate = null;
        String birthday = (String) map.get("birthday");
        try{
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            udate = format.parse(birthday);
        }catch (ParseException e){
            dataResponse.setMsg("日期格式有误");
            return dataResponse;
        }
        employee.setBirthday(udate);

        Date hdate = null;
        String hireDay = (String) map.get("hireDate");
        try{
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            hdate = format.parse(hireDay);
        }catch (ParseException e){
            dataResponse.setMsg("日期格式有误");
            return dataResponse;
        }
        employee.setHireDate(hdate);

        Date rdate = null;
        String resignDay = (String) map.get("resignDate");
        try{
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            rdate = format.parse(resignDay);
        }catch (ParseException e){
            dataResponse.setMsg("日期格式有误");
            return dataResponse;
        }
        employee.setResignDate(rdate);

        employee.setDescription(map.get("description").toString());
        employee.setUserName(map.get("userName").toString());
        employee.setGender(map.get("gender").toString());
        employee.setIdCard(map.get("idCard").toString());
        employee.setPhone(map.get("phone").toString());
        employee.setImgsetDir(map.get("imgsetDir").toString());
        employee.setProfilePhoto(map.get("profilePhoto").toString());
        dataResponse = employeeService.addEmployee(employee);
        return dataResponse;
    }


    @RequestMapping("/delete")
    private DataResponse<String> deleteEmployee(@RequestBody Map<String,Object> map){
        DataResponse<String> dataResponse = new DataResponse<>();
        int id = Integer.parseInt(map.get("id").toString());
        dataResponse = employeeService.deleteEmployee(id);
        return dataResponse;
    }

    @RequestMapping("/update")
    private DataResponse<String> updateEmployee(@RequestBody Map<String,Object> map){
        DataResponse<String> dataResponse = new DataResponse<>();
        Employee employee = new Employee();
        Date udate = null;
        String birthday = (String) map.get("birthday");
        dataResponse.setCode(14);
        try{
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            udate = format.parse(birthday);
        }catch (ParseException e){
            dataResponse.setMsg("日期格式有误");
            return dataResponse;
        }
        employee.setBirthday(udate);

        Date hdate = null;
        String hireDay = (String) map.get("hireDate");
        try{
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            hdate = format.parse(hireDay);
        }catch (ParseException e){
            dataResponse.setMsg("日期格式有误");
            return dataResponse;
        }
        employee.setHireDate(hdate);

        Date rdate = null;
        String resignDay = (String) map.get("resignDate");
        try{
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            rdate = format.parse(resignDay);
        }catch (ParseException e){
            dataResponse.setMsg("日期格式有误");
            return dataResponse;
        }
        employee.setResignDate(rdate);
        employee.setId(Integer.parseInt(map.get("id").toString()));
        employee.setDescription(map.get("description").toString());
        employee.setUserName(map.get("userName").toString());
        employee.setGender(map.get("gender").toString());
        employee.setIdCard(map.get("idCard").toString());
        employee.setPhone(map.get("phone").toString());
        employee.setImgsetDir(map.get("imgsetDir").toString());
        employee.setProfilePhoto(map.get("profilePhoto").toString());
        dataResponse = employeeService.updateEmployee(employee);
        return dataResponse;
    }

    @RequestMapping("/count")
    private DataResponse<Integer> getEmployeeNum(){
        DataResponse<Integer> dataResponse = new DataResponse<>();
        dataResponse.setData(employeeService.getEmployeeNum());
        return dataResponse;
    };

    @RequestMapping("/select")
    private DataResponse<List<Employee>> getEmployee(@RequestBody Map<String,Object> map){
        DataResponse<List<Employee>> dataResponse = new DataResponse<>();
        int from = 0;
        int num = 0;
        try{
            from = Integer.parseInt(map.get("from").toString());
            num = Integer.parseInt(map.get("num").toString());
        }catch(Exception e){
            dataResponse.setCode(1);
            dataResponse.setMsg("请求错误");
            return dataResponse;
        }
        dataResponse=employeeService.selectEmployee(from,num);
        return dataResponse;
    };

    @RequestMapping("/search")
    private DataResponse<List<Employee>> searchSystemUser(@RequestBody Map<String,Object> map){
        DataResponse<List<Employee>> dataResponse = new DataResponse<>();
        String userName;
        try{
            userName = map.get("userName").toString();
        }catch(Exception e){
            dataResponse.setMsg("请求错误");
            dataResponse.setCode(1);
            return dataResponse;
        }
        dataResponse = employeeService.selectEmployeeByName(userName);
        return dataResponse;
    }

    @RequestMapping("/changePhoto")
    private DataResponse<String> updateLogoImage(@RequestBody Map<String,Object> map){
        DataResponse<String> dataResponse = new DataResponse<>();
        dataResponse.setCode(0);
        int id=Integer.parseInt(map.get("id").toString());
        String profilePhoto=map.get("profilePhoto").toString();
        dataResponse = employeeService.updateProfile(id,profilePhoto);
        return dataResponse;
    }
}
